﻿namespace LeetCodeProject._0001_1000._001_100._21_30._025;

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}

public class Solution
{
    public ListNode ReverseKGroup(ListNode head, int k)
    {
        var stack = new Stack<ListNode>(k);
        var dummy = new ListNode(0) { next = head };
        var p = dummy;
        var q = dummy;
        for (int i = 0; i < k; i++)
        {
            q = q?.next;
        }

        while (q != null)
        {
            var h = p.next;
            var t = q.next;
            while (h != t)
            {
                stack.Push(h);
                h = h.next;
            }

            while (stack.Count > 0)
            {
                p = p.next = stack.Pop();
            }

            p.next = t;
            q = p;
            for (int i = 0; i < k; i++)
            {
                q = q?.next;
            }
        }

        return dummy.next;
    }
}